Skip to content

fixing factorial negative values#22278

Open
raushanprabhakar1 wants to merge 2 commits into
apache:mainfrom
raushanprabhakar1:fix/22270-fix-factorial-negative
Open

fixing factorial negative values#22278
raushanprabhakar1 wants to merge 2 commits into
apache:mainfrom
raushanprabhakar1:fix/22270-fix-factorial-negative

Conversation

@raushanprabhakar1
Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

PostgreSQL returns a domain error for factorial on negative inputs (ERROR: factorial of a negative number is undefined). DataFusion incorrectly returned 1 for negative values (e.g. factorial(-1)), which breaks PostgreSQL-aligned SQL semantics and can silently produce wrong results.

What changes are included in this PR?

  • factorial (core / datafusion-functions): Negative arguments now fail with an execution error and message aligned with PostgreSQL: factorial of a negative number is undefined, instead of returning 1.
  • Documentation: Updated the #[user_doc] description so it matches behavior (non-negative inputs; error on negative values and on overflow).
  • Tests: Added a sqllogictest in math.slt that expects this error for factorial(-1).

Note: Spark’s factorial in datafusion-spark is unchanged (PySpark-style nulls for out-of-domain values).

Are these changes tested?

Yes. A sqllogictest was added in datafusion/sqllogictest/test_files/math.slt for SELECT factorial(-1).

Are there any user-facing changes?

Yes. factorial with a negative argument now errors at execution time instead of returning 1. This matches PostgreSQL and fixes incorrect results; callers that relied on the old behavior would need to adjust.

No public Rust API breakage in exported types; behavior change is SQL/UDF semantics for negative inputs.

@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels May 16, 2026
@raushanprabhakar1
Copy link
Copy Markdown
Contributor Author

raushanprabhakar1 commented May 16, 2026

Hi @Dandandan , requesting you to review. Thanks.

@kumarUjjawal
Copy link
Copy Markdown
Contributor

Thank you @raushanprabhakar1 Looks good, just one required change, Can you run the script to update the docs as well ./dev/update_function_docs.sh

@github-actions github-actions Bot added the documentation Improvements or additions to documentation label May 17, 2026
@raushanprabhakar1
Copy link
Copy Markdown
Contributor Author

raushanprabhakar1 commented May 17, 2026

Hi @kumarUjjawal, Thanks for the review. updated the doc by running the script ./dev/update_function_docs.sh, as suggested by you. Please do review, Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation functions Changes to functions implementation sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PostgreSQL compatibility: factorial of a negative value should error

2 participants